cmake_minimum_required(VERSION 3.6.0)

# Check the internal variables.
if(NOT DEFINED __NXOS_BUILD_ARCH)
  message(FATAL_ERROR "Cannot use common.cmake directly!")
endif()

if(NOT DEFINED __NXOS_SDK_ROOT)
  set(__NXOS_SDK_ROOT ${CMAKE_CURRENT_LIST_DIR}/../..)
endif()

if(NOT DEFINED __NXOS_BUILD_TARGET_TRIPLE)
  set(__NXOS_BUILD_TARGET_TRIPLE ${__NXOS_BUILD_ARCH}-unknown-unknown-elf)
endif()

# Set System information.
if(DEFINED __NXOS_BUILD_PROCESSOR)
  set(CMAKE_SYSTEM_PROCESSOR ${__NXOS_BUILD_PROCESSOR})
else()
  set(CMAKE_SYSTEM_PROCESSOR ${__NXOS_BUILD_ARCH})
endif()

set(CMAKE_SYSTEM_NAME Generic-ELF)
set(CMAKE_SYSTEM_VERSION 1)
set(UNIX TRUE)

# Set the default compiler and tools.
if(NOT DEFINED NXOS_SDK_NO_COMPILER)
  if(NOT DEFINED CMAKE_ASM_COMPILER)
    set(CMAKE_ASM_COMPILER ${__NXOS_SDK_ROOT}/bin/clang)
  endif()

  if(NOT DEFINED CMAKE_C_COMPILER)
    set(CMAKE_C_COMPILER ${__NXOS_SDK_ROOT}/bin/clang)
  endif()

  if(NOT DEFINED CMAKE_CXX_COMPILER)
    set(CMAKE_CXX_COMPILER ${__NXOS_SDK_ROOT}/bin/clang++)
  endif()

  if(NOT DEFINED CMAKE_AR)
    set(CMAKE_AR ${__NXOS_SDK_ROOT}/bin/llvm-ar)
  endif()

  if(NOT DEFINED CMAKE_RANLIB)
    set(CMAKE_RANLIB ${__NXOS_SDK_ROOT}/bin/llvm-ranlib)
  endif()

  if(NOT DEFINED CMAKE_STRIP)
    set(CMAKE_STRIP ${__NXOS_SDK_ROOT}/bin/llvm-strip)
  endif()

  if(NOT DEFINED CMAKE_NM)
    set(CMAKE_NM ${__NXOS_SDK_ROOT}/bin/llvm-nm)
  endif()

  if(NOT DEFINED CMAKE_OBJCOPY)
    set(CMAKE_OBJCOPY ${__NXOS_SDK_ROOT}/bin/llvm-objcopy)
  endif()

  if(NOT DEFINED CMAKE_OBJDUMP)
    set(CMAKE_OBJDUMP ${__NXOS_SDK_ROOT}/bin/llvm-objdump)
  endif()
endif(NOT DEFINED NXOS_SDK_NO_COMPILER)

# Set the build target.
foreach(_lang IN ITEMS ASM C CXX)
  set(CMAKE_${_lang}_COMPILER_WORKS ON)
  set(CMAKE_${_lang}_COMPILER_FORCED ON)
  set(CMAKE_${_lang}_COMPILER_TARGET ${__NXOS_BUILD_TARGET_TRIPLE})
endforeach(_lang IN ITEMS ASM C CXX)

# Set the sysroot.
set(CMAKE_SYSROOT ${__NXOS_SDK_ROOT}/sysroots/${__NXOS_BUILD_ARCH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# Set the standard include directires.
set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_SYSROOT}/include)
set(CMAKE_ASM_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_SYSROOT}/include)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_SYSROOT}/include/c++/v1 ${CMAKE_SYSROOT}/include)

# Set the standard link libraries.
set(CMAKE_C_STANDARD_LIBRARIES -lnxos)
set(CMAKE_ASM_STANDARD_LIBRARIES -lnxos)

if("${__NXOS_BUILD_ARCH}" STREQUAL "x86")
  set(CMAKE_CXX_STANDARD_LIBRARIES "-lc++abi -lunwind -lnxos")
else()
  set(CMAKE_CXX_STANDARD_LIBRARIES -lnxos)
endif()

# Add custom compile/link options for nxos.
add_definitions(-DNXOS)
link_directories(${CMAKE_SYSROOT}/lib)
add_link_options(-Wl,-T${CMAKE_CURRENT_LIST_DIR}/../ldscripts/${__NXOS_BUILD_ARCH}.ld)

# todo: support exceptions
add_compile_options(-fno-exceptions)
add_link_options(-fno-exceptions)
